package demo.collection;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;

public class DemoMap {
	private Hashtable<Integer, String> hashTable = new Hashtable<Integer, String>();
	private HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
	private LinkedHashMap<Integer, String> linkedHashMap = new LinkedHashMap<Integer, String>();
	private TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>();

	public static void main(String[] args) {
		int length = 1000;
		DemoMap dm = new DemoMap();
		dm.init(length);
		
		System.out.println("Hash table");
		dm.print(dm.hashTable);
		System.out.println("===========");
		
		System.out.println("Hash map");
		dm.print(dm.hashMap);
		System.out.println("===========");
		
		System.out.println("Linked hash map");
		dm.print(dm.linkedHashMap);
		System.out.println("===========");
		
		System.out.println("tree map");
		dm.print(dm.treeMap);
		System.out.println("===========");
	}

	private void init(int length) {
		Random rd = new Random();
		for (int i = 0; i < length; i++) {
			int a = rd.nextInt();
			hashMap.put(i, a + "");
			hashTable.put(i, a + "");
			linkedHashMap.put(i, a + "");
			treeMap.put(i, a + "");
		}
	}

	private void print(Map<Integer, String> map) {
		long start = System.currentTimeMillis();
		Iterator<Integer> ite = map.keySet().iterator();
		while (ite.hasNext()){
			Integer key = ite.next();
			String value = map.get(key);
			System.out.print(" " + value);
		}
		long end = System.currentTimeMillis();
		System.out.println("");
		System.out.println("Thoi gian: " + (end - start));
	}
}
